package zzps.exeed.kernel.core;

import zzps.exeed.kernel.ExeedConfig;
import zzps.exeed.kernel.annotation.Component;
import zzps.exeed.kernel.annotation.Configuration;
import zzps.exeed.kernel.annotation.Inject;
import zzps.exeed.kernel.awareness.AspectAwarenessProcessor;
import zzps.exeed.kernel.awareness.EventListenerAwarenessProcessor;
import zzps.exeed.kernel.pandabuilder.ComponentPandaBuilder;
import zzps.exeed.kernel.pandabuilder.ConfigurationPandaBuilder;
import zzps.exeed.kernel.pandainjector.InjectPandaInjector;

public class AppContext {

    private PandaContainer pandaContainer;
    private ExeedConfig config;
    private ClassLoader classLoader;

    public AppContext(ClassLoader classLoader, ExeedConfig config) {
        pandaContainer = new PandaContainer();
        this.config = config;
        this.classLoader = classLoader;
        initialize();
    }

    public ExeedConfig config() {
        return config;
    }

    public PandaContainer container() {
        return pandaContainer;
    }

    private void initialize(){
        pandaContainer.addPandaBuilder(Configuration.class,new ConfigurationPandaBuilder(classLoader));
        pandaContainer.addPandaBuilder(Component.class, new ComponentPandaBuilder());
        pandaContainer.addPandaInjector(Inject.class, new InjectPandaInjector());

        pandaContainer.addAwarenessProcessor(new AspectAwarenessProcessor());
        pandaContainer.addAwarenessProcessor(new EventListenerAwarenessProcessor());
    }


    public void collectPanda(Class<?> clazz){
        ClassPathPandaEmbryoScanner classPathPandaEmbryoScanner = new ClassPathPandaEmbryoScanner(clazz.getClassLoader());
        classPathPandaEmbryoScanner.setProcessor(pandaContainer::collectPanda);
        classPathPandaEmbryoScanner.scan(clazz.getPackageName());
    }

    public void buildPanda(){
        pandaContainer.buildPanda();
    }

    public void start(){}

    public void stop() {}
}
